<template>
  <el-dialog :visible="isShow" title="分配权限" @close="closeEvent">
    <el-tree
      ref="tree"
      :data="roleList"
      :props="{ label: 'name' }"
      show-checkbox
      check-strictly
      node-key="id"
      :default-checked-keys="permIds"
    />
    <template #footer>
      <div>
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermission } from '@/api/permission.js'
import { changeTreeData } from '@/utils/index'
import { sysRoleId, sysRoleAssignPremPut } from '@/api/setting.js'
export default {
  name: '',
  props: { isShow: { type: Boolean, default: false }},
  data() {
    return {
      roleList: [],
      permIds: [],
      id: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取权限列表
    async getData() {
      const res = await sysPermission()
      // console.log(res)
      this.roleList = changeTreeData(res.data, '0')
    },
    // 关闭
    closeEvent() {
      this.$emit('update:isShow', false)
      this.$refs.tree.setCheckedKeys([])
    },
    // 获取角色详情
    async getRoleInfo(id) {
      this.id = id
      const res = await sysRoleId(id)
      console.log(res)
      this.permIds = res.data.permIds
      console.log(this.permIds)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    // 提交
    async submit() {
      console.log(this.$refs.tree.getCheckedKeys())
      await sysRoleAssignPremPut({
        id: this.id,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.closeEvent()
      this.$emit('getData')
    }
  }
}
</script>
<style scoped lang="scss"></style>
